package graphicalUserInterface;

import java.io.IOException;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.xml.sax.SAXException;

import exception.IllegalInput;
import exception.NullVariableFound;

import settings.MasterController;
import system.Init;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.GridData;

//@author U096819H
public class Loading extends Dialog {

	protected Shell shell;

	// @author U096819H
	public Loading(Shell parent, int style) {
		super(parent, style);
		setText("");
	}

	// @author U096819H
	public void open(MasterController mc, String message) {
		createContents(mc, message);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	// @author U096819H
	private void createContents(MasterController mc, String message) {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(226, 93);
		shell.setText(getText());
		shell.setLayout(new FormLayout());

		Composite composite = new Composite(shell, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(0, 63);
		fd_composite.right = new FormAttachment(0, 220);
		fd_composite.top = new FormAttachment(0);
		fd_composite.left = new FormAttachment(0);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new GridLayout(1, false));

		Label lblLoading = new Label(composite, SWT.NONE);
		lblLoading.setAlignment(SWT.CENTER);
		GridData gd_lblLoading = new GridData(SWT.CENTER, SWT.CENTER, true,
				true, 1, 1);
		gd_lblLoading.heightHint = 35;
		gd_lblLoading.widthHint = 200;
		lblLoading.setLayoutData(gd_lblLoading);
		lblLoading.setText(message);

		Init init = new Init();
		try {
			if (init.runInit(mc)) {
				lblLoading.setText("Press ESC to continue.");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
